<?php
/*
 * Created on Apr 30, 2012
 *
 * To change the template for this generated file go to
 * Window - Preferences - PHPeclipse - PHP - Code Templates
 */
require_once("dao/IDao.php");
require_once("model/Player.php");

class PlayerDAO implements IDao{
	var $conn;
	var $column = array("photo_id", "caption", "image_data_large", "image_data_medium", "image_data_thumb", "image_type", "image_size", "image_name", "player_id", "profile_pic");
	
	public function __construct($conn){
		$this->conn =& $conn;
	} 	
	public function getItem($id){
		$player = null;
		if($stmt = $this->conn->prepare("CALL PLAYER_READ_ITEM(?)")){
			$stmt->bind_param("i",$id);
			$stmt->execute();
			
			$stmt->bind_result($_playerid,$_firstname,$_mi,$_lastname,$_emailaddress,$_hometown,$_currentcity,$_bio,$_dob,$_verified,$_locked,$_admin,$_deleted);
			
			while($stmt->fetch()){
				$player = new Player();
				$player->playerid = $_playerid;
				$player->firstName = $_firstname;
				$player->MI = $_mi;
				$player->lastName = $_lastname;
				$player->emailAddress = $_emailaddress;
				$player->homeTown = $_hometown;
				$player->currentCity = $_currentcity;
				$player->bio = $_bio;
				$player->dob = $_dob;
				$player->verified = $_verified;
				$player->locked = $_locked;
				$player->admin = $_admin;
				$player->deleted = $_deleted;
			}			
		}
		else{
			throw new Exception("Statement preparation failed in class:" . __CLASS__);
		}
		$this->conn->close();
		
		return $player;
	}
 	public function saveItem($player){
 		try{
 			if(isset($player->playerid)){
 				if($stmt = $this->conn->prepare("CALL PLAYER_UPDATE_ITEM(?,?,?,?,?,?,?,?,?,?,?,?,?)")){
 					$stmt->bind_param("dssssssssdddd",
 							$player->playerid,
 							$player->firstName,
 							$player->MI,
 							$player->lastName,
 							$player->emailAddress,
 							$player->homeTown,
 							$player->currentCity,
 							$player->bio,
 							$player->dob,
 							$player->verified,
 							$player->locked,
 							$player->admin,
 							$player->deleted);
 						
 					$stmt->execute();
 					
 					if($stmt->error != ""){
 						printf("Error %s",$stmt->error);
 						exit();
 					}
 				}
 				else{
 					printf("Error occurred in PlayerMgmt::updatePlayer<br/>Details: %s",$stmt->error);
 					exit();
 				}	
 			}
 			else{
 				if($stmt = $this->conn->prepare("CALL PLAYER_CREATE(?,?,?,?,?,?,?,?,?,?)")){
 					$stmt->bind_param("ssssssssdd",
 							$player->firstName,
 							$player->MI,
 							$player->lastName,
 							$player->emailAddress,
 							$player->dob,
 							$player->homeTown,
 							$player->currentCity,
 							$player->bio,
 							$player->verified,
 							$player->locked);
 					$stmt->execute();
 					
 					if($stmt->error != ""){
 						printf("Error %s",$stmt->error);
 						exit();
 					}
 				}
 				else{
 					printf("Error occurred in PlayerMgmt::updatePlayer<br/>Details: %s",$stmt->error);
 					exit();
 				}
 			}
 			
			$stmt->close();	
 		}
		catch(Exception $e){
			throw new Exception($e->getMessage());
		}
		$this->conn->close();
 	}
 	public function deleteItem($id){}
 	public function searchPlayer($firstname,$mi,$lastname,$emailaddress,$hometown,$currentcity,$verified,$locked,$admin){
		$result = null;
		try{
			if($stmt = $this->conn->prepare("CALL PLAYER_READ_LIST(?,?,?,?,?,?,?,?,?)")){
				$stmt->bind_param("ssssssiii",$firstname,$mi,$lastname,$emailaddress,$hometown,$currentcity,$verified,$locked,$admin);
				$stmt->execute();
				
				$stmt->bind_result($_playerid,$_firstname,$_mi,$_lastname,$_emailaddress,$_dob,$_hometown,$_currentcity,$_verified,$_locked,$_admin,$_appearance);
				
				$result = array();
				
				while($stmt->fetch()){
					$plyr = new Player();
					$plyr->playerid = $_playerid;
					$plyr->firstName = $_firstname;
					$plyr->MI = $_mi;
					$plyr->lastName = $_lastname;
					$plyr->emailAddress = $_emailaddress;
					$plyr->dob = $_dob;
					$plyr->homeTown = $_hometown;
					$plyr->currentCity = $_currentcity;
					$plyr->verified = $_verified;
					$plyr->locked = $_locked;
					$plyr->admin = $_admin;
					$plyr->appearance = $_appearance;
					array_push($result,$plyr);
				}
				$stmt->close();	
			}
			else{
				printf("Error occurred in %s.<br/>Details: %s",__CLASS__,$stmt->error);
			}	
		}
		catch(Exception $e){
			throw new Exception($e->getMessage());
		}
		$this->conn->close();
		
		return $result;
	}
	public function getAppearance($playerId){
		$result = null;
		try{
			if($stmt = $this->conn->prepare("CALL PLAYER_READ_APPEARANCE(?)")){
				$stmt->bind_param("i",$playerId);
				$stmt->execute();
				$stmt->bind_result($_playerid,$_eventid,$_eventyear);
				$result = array();
				while($stmt->fetch()){
					$event = new Event();
					$event->eventId = $_eventid;
					$event->year = $_eventyear;
					array_push($result,$event);
				}
			}
			else{
				printf("Error occurred in preparing the statement for PlayerMgmt::getAppearance<br/>Details:%s", $stmt->error);
			}	
		}
		catch(Exception $e){
			echo $e->getMessage();
		}
		
		return $result;
	}

	public function getStandingByYear($playerId){
		$result = null;
		try{
			if($stmt = $this->conn->prepare("CALL PLAYER_STANDING_HISTORY(?)")){
				$stmt->bind_param("i",$playerId);
				$stmt->execute();
				$stmt->bind_result($_eventid,
									$_year,
									$_castleCreekOut,
									$_castleCreekIn,
									$_castleCreekTotal,
									$_castleCreekPar,
									$_castleCreekHoleInOne,
									$_stoneBridgeOut,
									$_stoneBridgeIn,
									$_stoneBridgeTotal,
									$_stoneBridgePar,
									$_stoneBridgeHoleInOne,
									$_millPondOut,
									$_millPondIn,
									$_millPondTotal,
									$_millPondPar,
									$_millPondHoleInOne,
									$_hagosOut,
									$_hagosIn,
									$_hagosTotal,
									$_hagosPar,
									$_hagosHoleInOne,
									$_grandTotal,
									$_grandPar,
									$_grandHoleInOne);
				$result = array();
				while($stmt->fetch()){
					$row = array();
					$row["eventid"] = $_eventid;
					$row["year"] = $_year;
					$row["castleCreekOut"] = $_castleCreekOut;
					$row["castleCreekIn"] = $_castleCreekIn;
					$row["castleCreekTotal"] = $_castleCreekTotal;
					$row["castleCreekPar"] = $_castleCreekPar;
					$row["castleCreekHoleInOne"] = $_castleCreekHoleInOne;
					$row["stoneBridgeOut"] = $_stoneBridgeOut;
					$row["stoneBridgeIn"] = $_stoneBridgeIn;
					$row["stoneBridgeTotal"] = $_stoneBridgeTotal;
					$row["stoneBridgePar"] = $_stoneBridgePar;
					$row["stoneBridgeHoleInOne"] = $_stoneBridgeHoleInOne;
					$row["millPondOut"] = $_millPondOut;
					$row["millPondIn"] = $_millPondIn;
					$row["millPondTotal"] = $_millPondTotal;
					$row["millPondPar"] = $_millPondPar;
					$row["millPondHoleInOne"] = $_millPondHoleInOne;
					$row["hagosOut"] = $_hagosOut;
					$row["hagosIn"] = $_hagosIn;
					$row["hagosTotal"] = $_hagosTotal;
					$row["hagosPar"] = $_hagosPar;
					$row["hagosHoleInOne"] = $_hagosHoleInOne;
					$row["grandTotal"] = $_grandTotal;
					$row["grandPar"] = $_grandPar;
					$row["grandHoleInOne"] = $_grandHoleInOne;
					array_push($result,$row);
				}
			}
			else{
				printf("Error occurred in preparing the statement for PlayerMgmt::getAppearance<br/>Details:%s", $stmt->error);
			}	
			$stmt->close();
		}
		catch(Exception $e){
			throw new Exception($e->getMessage());
		}
		$this->conn->close();
		
		return $result;
	}	
	
	public function getPhotosByPlayerId($playerId){
		$result = array();
		if($stmt = $this->conn->prepare("CALL GET_PHOTOS_BY_PLAYERID(?)")){
			$stmt->bind_param("i",$playerId);
			$stmt->execute();
				
			$stmt->bind_result($photo_id,$caption,$profile_pic);
			while($stmt->fetch()){
				$photo = new Photo();
				$photo->photoId = $photo_id;
				$photo->caption = $caption;
				$photo->profilePic = $profile_pic;
				array_push($result,$photo);
			}
			$stmt->close();
		}
		else{
			printf("<p>Error in getPhotosByPlayerId.<br/>Detail: %s</p>",$stmt->error);
		}
	
		return $result;
	}
	
	public function getProfilePic($playerId){
		$photo = null;
		if($stmt = $this->conn->prepare("CALL GET_PROFILE_PHOTO(?)")){
			$stmt->bind_param("i",$playerId);
			$stmt->execute();
			$stmt->store_result();
			$data = array();
			$params = array();
			foreach($this->column as $col_name){
				$params[] =& $data[$col_name];
			}
			$res = call_user_func_array(array($stmt,"bind_result"),$params);
			if(!$res){
				echo "bind_result() failed: ". $this->conn->error ."\n";
				exit();
			}
			else{
				$res = $stmt->fetch();
				if($res){
						
				}
				else{
						
				}
			}
			$photo = new Photo();
			$photo = $this->mapDataAndClass($photo,$data);
		}
		else{
			printf("<p>2. Failed in getProfilePic: %s</p>",$this->conn->error);
		}
		if($stmt->error != "");
		echo $stmt->error;
		$stmt->close();
	
		return $photo;
	}
	
	public function setProfilePic($playerId,$photoid){
		if($stmt = $this->conn->prepare("CALL PHOTO_SET_PROFILE_PIC(?,?)")){
			$stmt->bind_param("ii",$playerId,$photoid);
			$stmt->execute();
		}
		else{
			throw new Exception("setProfilePic didn\'t work.");
		}
		$stmt->close();
	}
	
	public function addPhoto($photo,$playerid,$isProfilePic){
		if($stmt = $this->conn->prepare("CALL PLAYER_PHOTO_CREATE(?,?,?,?,?,?,?,?,?,?)")){
			$null = null;
			$stmt->bind_param("sbbbssssii",$photo->caption,$null,$null,$null,$photo->imageType,$photo->imageSize,$photo->imageName,$photo->ext,$playerid,$isProfilePic);
			$stmt->send_long_data(1,$photo->imageDataLarge);
			$stmt->send_long_data(2,$photo->imageDataMed);
			$stmt->send_long_data(3,$photo->imageDataThumb);
			$stmt->execute();
				
			if($stmt->error != "")
				echo printf("Statement prepation failed at line %s in %s<br/>%s<br/>",__LINE__,__FILE__, $stmt->error);
				
			$stmt->close();
		}
		else{
			throw new Exception("Statement prepation failed at line ".__LINE__." in ".__FILE__.".");	
		}
	}
	
	private function mapDataAndClass($photo,$data){
		$photo->photoId = $data["photo_id"];
		$photo->caption = $data["caption"];
		$photo->imageDataLarge = $data["image_data_large"];
		$photo->imageDataMed =  $data["image_data_medium"];
		$photo->imageDataThumb =  $data["image_data_thumb"];
		$photo->imageType = $data["image_type"];
		$photo->imageSize = $data["image_size"];
		$photo->imageName = $data["image_name"];
		$photo->playerId = $data["player_id"];
		$photo->profilePic = $data["profile_pic"];
	
		return $photo;
	}
	
	public function __destruct(){
 	}
}
?>
